package com.heyqing.ems.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heyqing.ems.common.MemberType;
import com.heyqing.ems.domain.BossInfo;
import com.heyqing.ems.domain.EmployeeInfo;
import com.heyqing.ems.domain.LoginManagement;
import com.heyqing.ems.domain.ManagerInfo;
import com.heyqing.ems.mapper.BossInfoMapper;
import com.heyqing.ems.mapper.LoginManagementMapper;
import com.heyqing.ems.mapper.ManagerInfoMapper;
import com.heyqing.ems.po.ModifyPo;
import com.heyqing.ems.po.OpEmployeePo;
import com.heyqing.ems.po.QueryPo;
import com.heyqing.ems.service.EmployeeService;
import com.heyqing.ems.service.LoginService;
import com.heyqing.ems.service.ManagerService;
import com.heyqing.ems.util.IdUtil;
import com.heyqing.ems.vo.ManagerVo;
import com.heyqing.ems.vo.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * ClassName:ManagerServiceImpl
 * Package:com.heyqing.ems.service.impl
 * Description:
 *
 * @Date:2024/6/26
 * @Author:Heyqing
 */
@Service
@RequiredArgsConstructor
public class ManagerServiceImpl extends ServiceImpl<ManagerInfoMapper, ManagerInfo> implements ManagerService {

    private final EmployeeService employeeService;
    private final LoginManagementMapper loginManagementMapper;
    private final BossInfoMapper bossInfoMapper;

    @Override
    public Result getList(QueryPo queryPo) {
        if (Objects.isNull(queryPo)){
            return Result.fail();
        }
        Page<ManagerInfo> resPage = lambdaQuery()
                .like(ManagerInfo::getName, queryPo.getQuery())
                .page(new Page<ManagerInfo>(queryPo.getPageNum(), queryPo.getPageSize()));
        if (Objects.isNull(resPage) || Objects.isNull(resPage.getRecords()) || Objects.isNull(resPage.getTotal())){
            return Result.fail();
        }
        Map<String,Object> map = new HashMap<>();
        List<ManagerInfo> records = resPage.getRecords();
        List<ManagerVo> res = copy2Vo(records);
        map.put("list",res);
        map.put("total",resPage.getTotal());
        return Result.success(map);
    }

    @Override
    @Transactional
    public Result saveEmployee(OpEmployeePo opEmployeePo) {
        if (Objects.isNull(opEmployeePo)){
            return Result.fail();
        }
        Long id = opEmployeePo.getId();
        //服务代理
        EmployeeInfo employee = employeeService.getById(id);
        EmployeeInfo employeeInfo = new EmployeeInfo();
        BeanUtil.copyProperties(opEmployeePo,employeeInfo);
        if (Objects.isNull(employee)){
            // 添加
            employeeService.save(employeeInfo);
        }else {
            // 修改
            LambdaUpdateWrapper<EmployeeInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(EmployeeInfo::getId, employeeInfo.getId())
                    .set(EmployeeInfo::getName, employeeInfo.getName())
                    .set(EmployeeInfo::getPosition, employeeInfo.getPosition())
                    .set(EmployeeInfo::getDepartment, employeeInfo.getDepartment())
                    .set(EmployeeInfo::getManagerId, employeeInfo.getManagerId());
            employeeService.update(null, lambdaUpdateWrapper);
        }
        return Result.success();
    }

    @Override
    @Transactional
    public Result modify(ModifyPo modifyPo) {
        if (Objects.isNull(modifyPo)){
            return Result.fail();
        }
        Long id = modifyPo.getId();
        // 服务代理
        LoginManagement member = loginManagementMapper.selectById(id);
        LoginManagement loginManagement = new LoginManagement();
        BeanUtil.copyProperties(modifyPo,loginManagement);
        loginManagement.setType(MemberType.EMPLOYEE);
        if (Objects.isNull(member)){
            // 添加
            loginManagement.setId(IdUtil.getCurrentId());
            loginManagementMapper.insert(loginManagement);
        }else {
            // 修改
            loginManagementMapper.updateById(loginManagement);
        }
        return Result.success();
    }

    @Override
    @Transactional
    public Result delete(Long id) {
        employeeService.removeById(id);
        loginManagementMapper.deleteById(id);
        return Result.success();
    }

    /********************************************private**********************************************/
    private ManagerVo copy(ManagerInfo info){
        ManagerVo managerVo = new ManagerVo();
        BeanUtil.copyProperties(info,managerVo);
        BossInfo bossInfo = bossInfoMapper.selectById(info.getBossId());
        managerVo.setBoss(bossInfo.getName());
        return managerVo;
    }
    private List<ManagerVo> copy2Vo(List<ManagerInfo> records) {
        List<ManagerVo> res = new ArrayList<>();
        for (ManagerInfo record : records) {
            res.add(copy(record));
        }
        return res;
    }
}
